package com.fau.wmbj.server.storage;

import com.fau.wmbj.server.entity.OrderedEntity;
import com.fau.wmbj.server.utils.BaseAttributeConverter;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Hibernate;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 存储
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2024/6/27 下午4:12
 */
@Getter
@Setter
@Entity(name = "f_storage_config")
public class StorageConfig extends OrderedEntity<Long> implements Serializable {
    /**
     * 插件ID
     */
    @Column(nullable = false, updatable = false, unique = true, name = "plugin_id")
    private String pluginId;

    /**
     * 是否启用
     */
    @Column(nullable = false, name = "is_enabled")
    private Boolean isEnabled;

    /**
     * 属性
     */
    @Column(length = 4000, name = "attributes")
    @Convert(converter = AttributeConverter.class)
    private Map<String, String> attributes = new HashMap<>();

    /**
     * 获取属性值
     *
     * @param name 属性名称
     * @return 属性值
     */
    @Transient
    public String getAttribute(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        return getAttributes() != null ? getAttributes().get(name) : null;
    }

    /**
     * 类型转换 - 属性
     *
     * @author jiawei
     */
    @Converter
    public static class AttributeConverter extends BaseAttributeConverter<Map<String, String>> implements Serializable {

    }

    /**
     * Immutable.class
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
            return false;
        }
        StorageConfig that = (StorageConfig) o;
        return getId() != null && Objects.equals(getId(), that.getId());
    }

    /**
     * Immutable.class
     */
    @Override
    public int hashCode() {
        return getClass().hashCode();
    }

}
